package com.ninthgroup.benlailife.utils;

import android.content.Context;
import android.os.Build;
import android.os.Environment;


import com.ninthgroup.benlailife.BaseApp;
import com.ninthgroup.benlailife.config.AppConfig;

import java.io.File;

/**
 * Created by Administrator on 15-10-20.
 */
public class FileUtils {
    //获取根目录
    public static String getRootCache(){
        File externalCacheDir= BaseApp.getInstance().getExternalCacheDir();
        if (!externalCacheDir.exists()){
            //使用makedirs() 可以建立不只一层的directory,
            //用makedirs建立c:\\aa\\bb\\cc
            //若aa, bb不存在, makedirs会完整建立出所有的path!!
            externalCacheDir.mkdirs();
        }
        return externalCacheDir.getAbsolutePath();
    }
    /*
    * 获取图片的存储路径
    * */
    public static File getImageCache() {
        String imageCache=getRootCache()+File.separator+AppConfig.IMAGE_CACHE;
        File file=new File(imageCache);
        if (!file.exists()){
            //使用makedir() 可以建立一层的directory,
            //利用makedir()建立c:\\aa\\bb\\cc
            //若aa, bb不存在, 则makedir会失败
            file.mkdir();
        }
        return file;
    }
    public static File getRootCache(Context context) {
        File rootFile = null;
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
            rootFile = context.getExternalCacheDir();
        } else {
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                rootFile = new File(Environment.getExternalStorageDirectory() + File.separator + AppConfig.ROOT_CACHE);
            } else {
                rootFile = new File(Environment.getDataDirectory() + File.separator + context.getPackageName());
            }
            if (!rootFile.exists()) {
                rootFile.mkdirs();
            }
        }
        return rootFile;
    }
    //sdk/qianfeng/imageCache
    public static String getSaveBitmapPath(Context context) {
        String imagePath = getRootCache(context) + File.separator + AppConfig.IMAGE_CACHE;
        if (!new File(imagePath).exists()) {
            boolean mkdirs = new File(imagePath).mkdirs();
        }
        return imagePath;
    }
    /**
     * 获取数据库路径
     *
     * @param context
     * @return
     */
    public static String getDBPath(Context context) {
        String dbPath = getRootCache(context) + File.separator + AppConfig.DB_CACHE;
        if (!new File(dbPath).exists()) {
            boolean mkdirs = new File(dbPath).mkdirs();
        }

        return dbPath;
    }


}
